<!-- 护理项目列表页 -->
<template>

  <div class="min-h serveProject bg-wt">
    <SearchForm :searchData="pagination" @handleClear="handleClear" @handleReset="handleReset"
      @handleSearch="handleSearch"></SearchForm>
    <TableList :data="data" :total="total" :bordered="bordered" :hover="hover" :size="size" :pagination="pagination"
      @onPageChange="onPageChange" @handleBulid="handleBulid" @handleEdit="handleEdit"></TableList>
    <DialogForm ref="formRef" :visible="visible" :title="title" @handleClose="handleClose" @handleAdd="handleAdd" :data="formData" @handleEdit="handleEditForm"></DialogForm>
  </div>


</template>

<script setup lang="jsx">
import { onMounted, ref } from 'vue';
import SearchForm from './components/SearchForm.vue';
import TableList from './components/TableList.vue';
import { getProjectDetails, getProjectList, projectAdd, projectUpdate } from '@/api/serve';
import DialogForm from './components/DialogForm.vue';
import { MessagePlugin } from 'tdesign-vue-next';

const data = ref([]);
const total = ref(0);
const bordered = ref(true);
const hover = ref(false);
const size = ref('medium');
const visible = ref(false);
const title = ref('');
const formRef=ref({})

const handleRowClick = (e) => {
  console.log(e);
};

const pagination = ref({
  pageNum: 1,
  pageSize: 5,
});

const getPage = async () => {
  const result = await getProjectList(pagination.value)
  if (result.code == 200) {
    data.value = result.data.records
    total.value = result.data.total
  }
}

onMounted(() => {
  getPage()
})
const onPageChange = (pageInfo) => {
  pagination.value.pageNum = pageInfo.current
  pagination.value.pageSize = pageInfo.pageSize
  getPage()
}

const handleClear = (val) => {
  if (val = 'name') {
    pagination.value.name = ''
  } else if (val = 'status') {
    delete pagination.value.status
  }
}

const handleReset = () => {
  pagination.value = {
    pageNum: 1,
    pageSize: 5,
  }
  getPage()
}

const handleSearch = () => {
  getPage()
}
//新增按钮点击事件
const handleBulid = () => {
  title.value = '新增';
  visible.value = true;
}
const handleClose = () => {
  visible.value = false;
}

const handleAdd = async (formData) => {
  const result= await projectAdd(formData)
  if(result.code==200){
    MessagePlugin.success('操作成功')
    visible.value=false
    formRef.value.handleClear()
    getPage()
  }else{
    MessagePlugin.error(result.msg)
  }
}

const formData=ref({})
const handleEdit=async (val)=>{
  const result=await getProjectDetails(val)
  if(result.code==200){
    formData.value=result.data
    title.value='编辑'
    visible.value=true
  }else{
    MessagePlugin.success(result.msg)
  }
}

const handleEditForm=async (val)=>{
  const result=await projectUpdate(val)
  if(result.code==200){
    MessagePlugin.success('修改成功')
    formData.value=''
    visible.value=false
    getPage()
  }else{
    MessagePlugin.success(result.msg)
  }
}
</script>
